﻿Ext.define("PSI.User.MainForm", {
    createUI: function () {
        Ext.define("PSIOrgModel", {
            extend: "Ext.data.Model",
            fields: ["id", "text", "fullName", "orgCode", "leaf", "children"]
        });

        var orgStore = Ext.create("Ext.data.TreeStore", {
            model: "PSIOrgModel",
            proxy: {
                type: "ajax",
                url: "/User/GetAllOrgs"
            }
        });

        orgStore.on("load", this.onOrgStoreLoad, this);

        var orgTree = Ext.create("Ext.tree.Panel", {
            title: "组织机构",
            store: orgStore,
            rootVisible: false,
            useArrows: true,
            columns: {
                defaults: {
                    flex: 1,
                    sortable: false,
                    menuDisabled: true,
                    draggable: false
                },
                items: [
                    {
                        xtype: "treecolumn",
                        text: "名称",
                        dataIndex: "text"
                    },
                    {
                        text: "编码",
                        dataIndex: "orgCode"
                    }
                ]
            }
        });
        this.orgTree = orgTree;

        orgTree.on("select", function (rowModel, record) {
            this.onOrgTreeNodeSelect(record);
        }, this);

        orgTree.on("itemdblclick", function (v, record, item, index, e) {
            this.onEditOrg();
        }, this);

        Ext.define("PSIUser", {
            extend: "Ext.data.Model",
            fields: ["id", "loginName", "name", "enabled", "orgCode"]
        });
        var storeGrid = Ext.create("Ext.data.Store", {
            autoLoad: false,
            model: "PSIUser",
            data: []
        });

        var grid = Ext.create("Ext.grid.Panel", {
            title: "人员列表",
            columns: [
                { header: "id", dataIndex: "id", hidden: true },
                { header: "登录名", dataIndex: "loginName" },
                { header: "姓名", dataIndex: "name" },
                { header: "编码", dataIndex: "orgCode" },
                {
                    header: "是否允许登录", dataIndex: "enabled", renderer: function (value) {
                        return value ? "允许登录" : "<span style='color:red'>禁止登录</span>";
                    }
                }
            ],
            store: storeGrid
        });

        grid.on("itemdblclick", this.onEditUser, this);
        this.grid = grid;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "用户管理",
            tbar: [
                { text: "新增组织机构", handler: this.onAddOrg, scope: this },
                { text: "编辑组织机构", handler: this.onEditOrg, scope: this },
                { text: "删除组织机构", handler: this.onDeleteOrg, scope: this }, "-",
                { text: "新增用户", handler: this.onAddUser, scope: this },
                { text: "修改用户", handler: this.onEditUser, scope: this },
                { text: "删除用户", handler: this.onDeleteUser, scope: this }, "-",
                { text: "修改用户密码", handler: this.onEditUserPassword, scope: this }, "-",
                {
                    text: "关闭", iconCls: "psi-button-exit", handler: function () {
                        location.replace("/");
                    }
                }
            ],
            items: [
                {
                    region: "center", xtype: "container", layout: "fit", border: 0,
                    items: [grid]
                },
                {
                    xtype: "container",
                    region: "west",
                    layout: "fit",
                    width: 300,
                    minWidth: 200,
                    maxWidth: 350,
                    split: true,
                    border: 0,
                    items: [orgTree]
                }
            ]
        });

        this.userGridPanel = panel;

        this.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    getGrid: function () {
        return this.grid;
    },

    getUserGridPanel: function () {
        return this.userGridPanel;
    },

    onAddOrg: function () {
        var form = Ext.create("PSI.User.OrgEditForm", {});
        form.show();
    },

    onEditOrg: function () {
        var tree = this.orgTree;
        var item = tree.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的组织机构");
            return;
        }

        var org = item[0].data;

        var form = Ext.create("PSI.User.OrgEditForm", {});
        form.show(org);
    },

    onDeleteOrg: function () {
        var tree = this.orgTree;
        var item = tree.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要删除的组织机构");
            return;
        }

        var org = item[0].data;

        PSI.MsgBox.confirm("请确认是否删除组织机构 <span style='color:red'>" + org.fullName + "</span> ?",
            function () {
                Ext.getBody().mask("正在删除中...");
                Ext.Ajax.request({
                    url: "/User/DeleteOrg",
                    method: "POST",
                    params: { id: org.id },
                    callback: function (options, success, response) {
                        Ext.getBody().unmask();

                        if (success) {
                            var data = Ext.JSON.decode(response.responseText);
                            if (data.success) {
                                PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                    window.location.reload();
                                });
                            } else {
                                PSI.MsgBox.showInfo(data.msg);
                            }
                        } else {
                            PSI.MsgBox.showInfo("网络错误", function () {
                                window.location.reload();
                            });
                        }
                    }

                });
            });
    },

    // private
    onAddUser: function () {
        var editFrom = Ext.create("PSI.User.UserEditForm");
        editFrom.show();
    },

    onEditUser: function () {
        var item = this.grid.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的用户");
            return;
        }

        var user = item[0].data;

        var tree = this.orgTree;
        var node = tree.getSelectionModel().getSelection();
        if (node && node.length == 1) {
            var org = node[0].data;

            user.orgId = org.id;
            user.orgName = org.fullName;
        }

        var editFrom = Ext.create("PSI.User.UserEditForm", {});
        editFrom.show(user);
    },

    onEditUserPassword: function () {
        var item = this.grid.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要修改密码的用户");
            return;
        }

        var user = item[0].data;
        var editFrom = Ext.create("PSI.User.ChangeUserPasswordForm", {});
        editFrom.show(user);
    },

    onDeleteUser: function () {
        var item = this.grid.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            PSI.MsgBox.showInfo("请选择要删除的用户");
            return;
        }

        var user = item[0].data;

        PSI.MsgBox.confirm("请确认是否删除用户 <span style='color:red'>" + user.name + "</span> ?",
            function () {
                Ext.getBody().mask("正在删除中...");
                Ext.Ajax.request({
                    url: "/User/DeleteUser",
                    method: "POST",
                    params: { id: user.id },
                    callback: function (options, success, response) {
                        Ext.getBody().unmask();

                        if (success) {
                            var data = Ext.JSON.decode(response.responseText);
                            if (data.success) {
                                PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                    window.location.reload();
                                });
                            } else {
                                PSI.MsgBox.showInfo(data.msg);
                            }
                        } else {
                            PSI.MsgBox.showInfo("网络错误", function () {
                                window.location.reload();
                            });
                        }
                    }
                });
            });
    },

    // private
    onOrgTreeNodeSelect: function (rec) {
        if (!rec) {
            return;
        }

        var org = rec.data;
        if (!org) {
            return;
        }

        var grid = this.getGrid();
        var panel = this.getUserGridPanel();

        grid.setTitle(org.fullName + " - 人员列表");

        grid.getEl().mask("数据加载中...");

        Ext.Ajax.request({
            url: "/User/GetUsers",
            params: { orgId: org.id },
            method: "POST",
            callback: function (options, success, response) {
                var store = grid.getStore();

                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);

                    for (var i = 0; i < data.length; i++) {
                        var item = data[i];
                        store.add({
                            id: item.id, loginName: item.loginName,
                            name: item.name, enabled: item.enabled,
                            orgCode: item.orgCode
                        });
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误", function () {
                        location.reload();
                    });
                }

                grid.getEl().unmask();
            }
        });
    },

    // private
    onOrgStoreLoad: function () {
        var tree = this.orgTree;
        var root = tree.getRootNode();
        if (root) {
            var node = root.firstChild;
            if (node) {
                this.onOrgTreeNodeSelect(node);
            }
        }
    }
});